<template>
  <div>
    <el-card>
      <el-button @click="addSort()" type="primary">添加分类</el-button>
    </el-card>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column align="center" prop="name" label="分类名称"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="toedit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteSort(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="60%">
      <el-form>
        <el-form-item label="添加分类：">
          <el-input style="width:150px" v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { catList, catDel, catAdd } from "@/api/activities";
export default {
  data() {
    return {
      tableData: [],
      title: "添加分类",
      dialogVisible: false,
      form: {
        name: "",
        catId: ""
      }
    };
  },
  methods: {
    save() {
      if (this.form.name == "") {
        this.$message.error("添加分类名称");
        return;
      }
      catAdd(this.form).then(res => {
        this.$message(res.data.data);
        this.dialogVisible = false;
        this.getData();
      });
    },
    addSort() {
      this.title = "添加分类";
      this.dialogVisible = true;
      this.form = {
        name: ""
      };
    },
    toedit(item) {
      this.title = "编辑分类";
      catList({ catId: item.catId }).then(res => {
        this.form.catId = res.data.data.catId;
        this.form.name = res.data.data.name;
      });
      this.dialogVisible = true;
    },
    deleteSort(item) {
      catDel({catId:item.catId}).then(res => {
        this.$message(res.data.data);
        this.getData();
      });
    },
    getData() {
      catList().then(res => {
        this.tableData = res.data.data;
      });
    }
  },
  mounted() {
    this.getData();
  }
};
</script>